﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmDinhKhoanUyNhiemChi : Form
    {
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();

        private UyNhiemChi uynhiemchi;
        public UyNhiemChi UyNhiemChi
        {
            set { this.uynhiemchi = value; }
            get { return this.uynhiemchi; }
        }

        public FrmDinhKhoanUyNhiemChi()
        {
            InitializeComponent();
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;
            return listChiNhanhCongTy;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> listNhaCungCap = (List<NhaCungCap>)resultObj.Result;
            return listNhaCungCap;
        }

        public void AddRowToDGV()
        {
            //add row tk "có 1121 | 1111"
            DataGridViewRow row = new DataGridViewRow();

            DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            lstTaiKhoanKeToan = (from tk in GetAllTaiKhoanKeToan()
                                 select tk).ToList();

            if (lstTaiKhoanKeToan.Count > 0)
            {
                cboCell.DataSource = lstTaiKhoanKeToan;
                cboCell.DisplayMember = "MaSoTK";
                cboCell.ValueMember = "Id";
                row.Cells.Add(cboCell);

                cboCell = new DataGridViewComboBoxCell();
                cboCell.Items.Add("Nợ");
                cboCell.Items.Add("Có");
                cboCell.Value = "Có";
                row.Cells.Add(cboCell);

                DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                cell.Value = "";
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = 0;
                row.Cells.Add(cell);

                dgvDinhKhoan.Rows.Add(row);

                dgvDinhKhoan.Rows[0].Cells[3].Value = 0;
            }
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan.CurrentRow != null)
            {
                if (dgvDinhKhoan.CurrentRow.Index != (dgvDinhKhoan.Rows.Count - 1))
                {
                    dgvDinhKhoan.Rows.RemoveAt(dgvDinhKhoan.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoan.Rows[dgvDinhKhoan.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    dgvDinhKhoan.Rows[0].Cells[3].Value = result;
                }
            }
        }

        private void dgvDinhKhoan_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoan.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoan.ClearSelection();
                    dgvDinhKhoan.CurrentCell = dgvDinhKhoan.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoan.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        private void dgvDinhKhoan_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tkkt in GetAllTaiKhoanKeToan()
                                                          where tkkt.Id == Convert.ToInt32(dgvDinhKhoan.Rows[e.RowIndex].Cells[0].Value.ToString())
                                                          select tkkt).ToList();
                string tenTK = lstTaiKhoanKeToan[0].TenTK;

                dgvDinhKhoan.Rows[e.RowIndex].Cells[2].Value = tenTK;
                dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            if (e.ColumnIndex == 3 && e.RowIndex > -1)
            {
                if (dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value == null)
                    dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            long tongBaBaMot = 0;
            for (int j = 1; j < dgvDinhKhoan.Rows.Count - 1; j++)
            {
                if (dgvDinhKhoan.Rows[j].Cells[3].Value != null)
                {
                    tongBaBaMot += Convert.ToInt64(dgvDinhKhoan.Rows[j].Cells[3].Value.ToString());
                }
            }
            dgvDinhKhoan.Rows[0].Cells[3].Value = tongBaBaMot;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            long sotien = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[3].Value.ToString());
            if (sotien != uynhiemchi.SoTien)
            {
                MessageBox.Show("Vui lòng điều chỉnh số tiền còn thiếu cho đúng !", "Thông báo");
                return;
            }

            EditOrRemoveDinhKhoan(uynhiemchi.MaSo);
            ResultObj resultObj = new ResultObj();

            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan.Rows[i].Cells[3].FormattedValue.ToString() != "0")
                {
                    ButToan buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = 0;
                    buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[3].Value.ToString());
                    resultObj = buttoanMgt.SaveNewButToan(buttoan);

                    buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[3].Value.ToString());
                    buttoan.GhiCo = 0;
                    resultObj = buttoanMgt.SaveNewButToan(buttoan);
                }

            }

            if (resultObj.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");
                this.Close();
            }
            else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");
        }

        private void FrmDinhKhoanUyNhiemChi_Load(object sender, EventArgs e)
        {
            dgvDinhKhoan.Columns["colSoTien"].ReadOnly = false;

            cboSoTK.DataSource = GetAllTaiKhoanKeToan();
            cboSoTK.DisplayMember = "MaSoTK";
            cboSoTK.ValueMember = "Id";

            txtSoChungTu.Text = uynhiemchi.MaSo;
            txtNoiDung.Text = uynhiemchi.NoiDung;

            List<NhaCungCap> lstNhaCungCap = (from cn in GetAllNhaCungCap()
                                              where cn.Id == uynhiemchi.IdNhaCungCap
                                              select cn).ToList();
            if (lstNhaCungCap.Count > 0)
                txtMSKH.Text = lstNhaCungCap[0].TenNhaCungCap;


            List<ChiNhanhCongTy> lstChiNhanhCongTy = (from cn in GetAllChiNhanhCongTy()
                                                      where cn.Id == uynhiemchi.IdChiNhanhCongTy
                                                      select cn).ToList();
            if (lstChiNhanhCongTy.Count > 0)
                txtChiNhanh.Text = lstChiNhanhCongTy[0].TenChiNhanh;

            AddRowToDGV();
        }

        private void dgvDinhKhoan_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = false;
        }
    }
}
